package avicit.bdp.dms.tdm.rest;

import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.excel.ExcelUtil;
import avicit.platform6.core.properties.PlatformConstant;
import avicit.platform6.core.rest.msg.QueryReqBean;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;
import avicit.platform6.commons.utils.StringUtils;
import avicit.platform6.commons.utils.ComUtil;
import avicit.bdp.dms.tdm.dto.BdpAssetDemandDTO;
import avicit.bdp.dms.tdm.dto.BdpAssetDemandImportDTO;
import avicit.platform6.core.excel.imp.entity.ExcelImportResult;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import avicit.bdp.dms.tdm.service.BdpAssetDemandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.authz.annotation.Logical;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @金航数码科技有限责任公司
 * @作者：ZiyanYan
 * @邮箱：yanzy@avic.com
 * @创建时间： 2024-03-19 07:56
 * @类说明：数据资产需求表Rest
 * @修改记录：
 */
@RestController
@Api(tags = "bdpAssetDemand", value = "数据资产需求表")
@RequestMapping("/api/bdp/dms/bdpassetdemands")
	public class BdpAssetDemandRest {

	private static final Logger logger = LoggerFactory.getLogger(BdpAssetDemandRest.class);

	@Autowired
	private BdpAssetDemandService bdpAssetDemandService;


	/**
	 * 按条件分页查询
	 *
	 * @param queryReqBean 查询条件
	 * @return ResponseMsg<QueryRespBean<BdpAssetDemandDTO>>
	 */
	@PostMapping("/find-list-by-page/v1")
	@ApiOperation(value = "按条件分页查询")
//	@RequiresPermissions("bdpAssetDemand:view")
	public ResponseMsg<QueryRespBean<BdpAssetDemandDTO>> findListByPage(@ApiParam(value = "查询条件", name = "queryReqBean") @RequestBody QueryReqBean<BdpAssetDemandDTO> queryReqBean) {
		ResponseMsg<QueryRespBean<BdpAssetDemandDTO>> responseMsg = new ResponseMsg<>();
		QueryRespBean<BdpAssetDemandDTO> result = bdpAssetDemandService.findListByPage(queryReqBean);
		responseMsg.setResponseBody(result);
        SysLogUtil.log("资产需求管理模块", "查询数据资产需求信息", PlatformConstant.OpType.select);
		return responseMsg;
	}

	/**
	 * 按条件不分页查询
	 *
	 * @param queryReqBean 查询条件
	 * @return ResponseMsg<List<BdpAssetDemandDTO>>
	 */
	@PostMapping("/find-list/v1")
	@ApiOperation(value = "按条件不分页查询")
	public ResponseMsg<List<BdpAssetDemandDTO>> findList(@ApiParam(value = "查询条件", name = "queryReqBean") @RequestBody QueryReqBean<BdpAssetDemandDTO> queryReqBean) {
		ResponseMsg<List<BdpAssetDemandDTO>> responseMsg = new ResponseMsg<>();
		List<BdpAssetDemandDTO> result = bdpAssetDemandService.findList(queryReqBean);
		responseMsg.setResponseBody(result);
        SysLogUtil.log("资产需求管理模块", "查询数据资产需求信息", PlatformConstant.OpType.select);
		return responseMsg;
	}

	/**
	 * 通过主键查询单条记录
	 *
	 * @param id 主键id
	 * @return ResponseMsg<BdpAssetDemandDTO>
	 */
	@GetMapping("/get/{id}/v1")
	@ApiOperation(value = "通过主键查询单条记录")
//    @RequiresPermissions("bdpAssetDemand:edit")
	public ResponseMsg<BdpAssetDemandDTO> get(@ApiParam(value = "主键id", name = "id") @PathVariable("id") String id) {
		ResponseMsg<BdpAssetDemandDTO> responseMsg = new ResponseMsg<>();
		BdpAssetDemandDTO bdpAssetDemand = bdpAssetDemandService.get(id);
		responseMsg.setResponseBody(bdpAssetDemand);
		SysLogUtil.log4Query(bdpAssetDemand);
		return responseMsg;
	}

	/**
	 * 保存对象
	 *
	 * @param bdpAssetDemand 保存对象
	 * @return ResponseMsg<String>
	 */
	@PostMapping("/save/v1")
	@ApiOperation(value = "新增对象")
//	@RequiresPermissions(value={"bdpAssetDemand:edit","bdpAssetDemand:add"},logical= Logical.OR)
	public ResponseMsg<String> save(@ApiParam(value = "保存对象", name = "bdpAssetDemand") @Validated @RequestBody BdpAssetDemandDTO bdpAssetDemand) {
		ResponseMsg<String> responseMsg = new ResponseMsg<>();
		if (StringUtils.isEmpty(bdpAssetDemand.getId())) {
			responseMsg.setResponseBody(bdpAssetDemandService.insert(bdpAssetDemand));
		}else{
			responseMsg.setResponseBody(String.valueOf(bdpAssetDemandService.updateAll(bdpAssetDemand)));
		}
		return responseMsg;
	}

    /**
	 * 修改部分对象字段
	 *
	 * @param bdpAssetDemand 修改对象
	 * @return ResponseMsg<Integer>
	 */
	@PutMapping("/update-sensitive/v1")
	@ApiOperation(value = "修改部分对象字段")
	public ResponseMsg<Integer> updateSensitive(@ApiParam(value = "修改对象", name = "bdpAssetDemand")  @RequestBody BdpAssetDemandDTO bdpAssetDemand) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(bdpAssetDemandService.updateSensitive(bdpAssetDemand));
		return responseMsg;
	}

	/**
	 * 修改对象需求状态(接收)
	 *
	 * @param bdpAssetDemand 修改对象
	 * @return ResponseMsg<Integer>
	 */
	@PutMapping("/update-demand-status-custom/v1")
	@ApiOperation(value = "修改对象需求状态")
	public ResponseMsg<Integer> updateDemandStatusCustom(@ApiParam(value = "修改对象", name = "bdpAssetDemand")  @RequestBody BdpAssetDemandDTO bdpAssetDemand) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		bdpAssetDemand.setDemandStatus(2);
		responseMsg.setResponseBody(bdpAssetDemandService.updateSensitive(bdpAssetDemand));
		return responseMsg;
	}

	/**
	 * 修改全部对象字段
	 *
	 * @param bdpAssetDemand 修改对象
	 * @return ResponseMsg<Integer>
	 */
	@PutMapping("/update-all/v1")
	@ApiOperation(value = "修改全部对象字段")
	public ResponseMsg<Integer> updateAll(@ApiParam(value = "修改对象", name = "bdpAssetDemand") @Validated @RequestBody BdpAssetDemandDTO bdpAssetDemand) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(bdpAssetDemandService.updateAll(bdpAssetDemand));
		return responseMsg;
	}

	/**
	 * 按主键单条删除
	 *
	 * @param id 主键id
	 * @return ResponseMsg<Integer>
	 */
	@DeleteMapping("/delete-by-id/{id}/v1")
	@ApiOperation(value = "按主键单条删除")
	public ResponseMsg<Integer> deleteById(@ApiParam(value = "主键id", name = "id") @PathVariable("id") String id) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(bdpAssetDemandService.delete(id));
		return responseMsg;
	}

	/**
	 * 批量删除
	 *
	 * @param ids 逗号分隔的id串
	 * @return ResponseMsg<Integer>
	 */
	@DeleteMapping("/delete-by-ids/v1")
	@ApiOperation(value = "批量删除")
//	@RequiresPermissions("bdpAssetDemand:delete")
	public ResponseMsg<Integer> deleteByIds(@ApiParam(value = "id数组", name = "ids") @RequestBody String[] ids) {
		ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
		responseMsg.setResponseBody(bdpAssetDemandService.deleteByIds(ids));
		return responseMsg;
	}


    @GetMapping("/download-template/v1")
	@ApiOperation(value = "模板下载")
	public void downloadTemplate(HttpServletResponse response) throws IOException {
		// 输出
		ExcelUtil.downloadTemplate(response, BdpAssetDemandImportDTO.class,"导入模板");
	}

	/**
	 * 导入数据
	 * @param multipartFile 导入文件
	 * @return
	 */
	@PostMapping(value = "/import-data/v1", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	@ApiOperation(value = "导入文件")
//	@RequiresPermissions("bdpAssetDemand:import")
	public ResponseMsg<Object> importDataV2(@ApiParam(value = "Excel 文件", name = "impExcelFile") @RequestPart("impExcelFile") MultipartFile multipartFile,@RequestParam("formData") String formData) throws IOException {
		ResponseMsg<Object> responseMsg = new ResponseMsg<>();
		List<Map<String, String>> excelList = ExcelUtil.importExcel(multipartFile.getInputStream(), BdpAssetDemandImportDTO.class);
		ExcelImportResult excelImportResult = bdpAssetDemandService.importData(excelList,formData);
		responseMsg.setResponseBody(excelImportResult);
		return responseMsg;
	}

	/**
	 * 按条件导出所有
	 * @param queryReqBean 查询条件
	 * @return
	 */
	@PostMapping("/export-data/v1")
	@ApiOperation(value = "按条件导出所有")
//	@RequiresPermissions("bdpAssetDemand:export")
	public void exportData(@ApiParam(value = "查询条件", name = "queryReqBean") @RequestBody QueryReqBean<BdpAssetDemandDTO> queryReqBean, HttpServletResponse response) throws IOException {
        List<BdpAssetDemandDTO> list = bdpAssetDemandService.findList(queryReqBean);
		// 输出
		ExcelUtil.exportExcel(response, list, BdpAssetDemandDTO.class, "导出数据", "sheet1");
		SysLogUtil.log(new BdpAssetDemandDTO().getLogTitle(), new BdpAssetDemandDTO().getLogTitle()+"共导出"+list.size()+"条数据", PlatformConstant.OpType.fileExport, PlatformConstant.OpResult.success, ComUtil.getId(), "BDP_ASSET_DEMAND");
	}
}
